package cn.lg.soar.core.dto;

import cn.lg.soar.core.enums.PermitLevelEnum;
import org.springframework.util.Assert;

import java.util.Collections;
import java.util.Set;

/**
 * @author luguoxiang 469599794@qq.com
 * @Date: 2023/12/6 12:46
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class PermitLevel {

    private final PermitLevelEnum level;

    private final Set<Integer> userTypes;

    protected PermitLevel(Set<Integer> userTypes) {
        this.level = PermitLevelEnum.user_type;
        this.userTypes = userTypes;
    }

    protected PermitLevel(PermitLevelEnum level) {
        this.level = level;
        this.userTypes = Collections.emptySet();
    }

    public PermitLevelEnum getLevel() {
        return level;
    }

    public Set<Integer> getUserTypes() {
        return userTypes;
    }

    public static PermitLevel valueOf(PermitLevelEnum level) {
        return switch (level) {
            case closed -> PermitLevel.closed;
            case authorized -> PermitLevel.authorized;
            case authenticated -> PermitLevel.authenticated;
            case identifiable -> PermitLevel.identifiable;
            case anon -> PermitLevel.anon;
            default -> throw new IllegalArgumentException("Invalid permit level: " + level);
        };
    }

    public static PermitLevel valueOf(Set<Integer> userTypes) {
        Assert.notNull(userTypes, "userTypes must not be empty");
        return new PermitLevel(userTypes);
    }

    public static final PermitLevel closed = new PermitLevel(PermitLevelEnum.closed);
    public static final PermitLevel authorized = new PermitLevel(PermitLevelEnum.authorized);
    public static final PermitLevel authenticated = new PermitLevel(PermitLevelEnum.authenticated);
    public static final PermitLevel identifiable = new PermitLevel(PermitLevelEnum.identifiable);
    public static final PermitLevel anon = new PermitLevel(PermitLevelEnum.anon);

}
